﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SearchAssembly
{
    public partial class frmMaster : Form
    {
        private bool dragging;
        private Point pointClicked;

        /// <summary>
        /// Current page data, when displayed as wizard page
        /// </summary>
        public WizardData.PageDataDataTable pageData = null;

        /// <summary>
        /// Current dialog data, when displayed as wizard dialog
        /// </summary>
        public System.Data.DataTable dialogData = null;

        /// <summary>
        /// Constructor
        /// </summary>
        public frmMaster()
        {
            InitializeComponent();
        }

        private void frmMaster_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                dragging = true;
                pointClicked = new Point(e.X, e.Y);
            }
            else
            {
                dragging = false;
            }
        }

        private void frmMaster_MouseMove(object sender, MouseEventArgs e)
        {
            if (dragging)
            {
                Point pointMoveTo;
                pointMoveTo = this.PointToScreen(new Point(e.X, e.Y));

                pointMoveTo.Offset(-pointClicked.X, -pointClicked.Y);

                this.Location = pointMoveTo;
            }
        }

        private void frmMaster_MouseUp(object sender, MouseEventArgs e)
        {
            dragging = false;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to exit", "Application Exit Confirmation", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {
                this.Close();
                Application.Exit();
            }
        }

        /// <summary>
        /// Display the form as wizard page
        /// </summary>
        /// <param name="nextPage">Page to be displayed</param>
        public void ShowPage(frmMaster nextPage)
        {
            System.Threading.Thread newPageThread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(RunPage));
            newPageThread.SetApartmentState(System.Threading.ApartmentState.STA);
            newPageThread.Start(nextPage);
            this.Close();
        }

        /// <summary>
        /// Display the form as dialog
        /// </summary>
        /// <param name="dialog2Show">Page to be displayed as dialog</param>
        /// <param name="DialogData2Load">Data for the dialog when it is being loaded</param>
        /// <param name="Modal">Whether to display dialog as modal</param>
        public void ShowDialog(frmMaster dialog2Show, System.Data.DataTable DialogData2Load, bool Modal)
        {
            dialog2Show.dialogData = new DataTable();
            dialog2Show.dialogData = DialogData2Load.Copy();

            if (Modal)
                dialog2Show.ShowDialog(this);
            else
                dialog2Show.Show(this);
        }

        private void RunPage(object page2Run)
        {
            Application.Run((frmMaster)page2Run);
            try
            {

            }
            catch (Exception)
            {
                // ignore
                throw;
            }
        }
    }
}
